package reflect;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Scanner;

public class ReflectDemo4 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
        Person p = new Person();
        p.sayHello();
        String s =p.getName();
        System.out.println(s);

        Class cls = Class.forName("reflect.Person");
        Object obj = cls.newInstance();
        Method m =cls.getMethod("sayHello");
        m.invoke(obj);

        Method method1 = cls.getMethod("getName");
        Object returnValue = method1.invoke(obj);
        System.out.println("返回值："+returnValue);

        Scanner scan = new Scanner(System.in);
        System.out.println("请输入类名");
        String classname = scan.nextLine();
        System.out.println("请输入方法名");
        String methodName = scan.nextLine();
        Class cls2 = Class.forName(classname);
        Object o =cls2.newInstance();
        Method mo =cls2.getMethod(methodName);
        mo.invoke(o);
    }
}
